package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            return new Result(true,"查询角色成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestParam List<Integer> permissionIds,@RequestParam List<Integer> menuIds, @RequestBody Role role){
        try {

            roleService.addRole(permissionIds,menuIds,role);
            return new Result(true,"新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增角色失败");
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestParam(value ="permissionIds")List<Integer> permissionIds,@RequestParam(value ="menuIds") List<Integer> menuIds, @RequestBody Role role){
        try {
            roleService.updateRole(permissionIds,menuIds,role);
            return new Result(true,"修改角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改角色失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,"查询角色成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteRoleById(id);
            return new Result(true,"删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"删除角色失败");
        }
    }
}
